package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.Quest;


/**
 * Servlet implementation class Questionnaire
 */
@WebServlet(name="Questionnaire", urlPatterns={"/JSP/Maha/Questionnaire"})
public class Questionnaire extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Questionnaire() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String titre=request.getParameter("titre");		 
		String    nbrQL=(String)request.getParameter("nbrQL");
		String   nbrQM=(String)request.getParameter("nbrQM");
		String   nbrQU=(String)request.getParameter("nbrQU");
		
		int nbrQLInt=Integer.valueOf(nbrQL).intValue();
		int nbrQMInt=Integer.valueOf(nbrQM).intValue();
		int nbrQUInt=Integer.valueOf(nbrQU).intValue();
		
		
		TreeMap tm=new TreeMap();
		
		List QL=new ArrayList();
		String QLparam;
		String QLO;
		String QMO;
		String QUO;
		
		for(int i=0;i<nbrQLInt;i++)
			{
			QLparam=(String)request.getParameter("libre"+i);
			QLO=    (String)request.getParameter("QLO"+i);
			
			QL.add(QLparam);
			tm.put(QLO+"QL",QLparam);
			
			}
		
		HashMap QM=new HashMap();
		String QMparam;
		String QMpos;
		for(int i=0;i<nbrQMInt;i++)
			{
			QMparam=(String)request.getParameter("mult"+i);
			QMpos=(String)request.getParameter("nbrPosM"+i);
			int indiceOrdre=i+nbrQLInt;
			
			QMO=    (String)request.getParameter("QMO"+indiceOrdre);
		
			QM.put(QMparam,QMpos);
			tm.put(QMO+"QM", QMparam);
			}
			
		HashMap QU=new HashMap();
		String QUparam;
		String QUpos;
		for(int j=0;j<nbrQUInt;j++)
			{
			QUparam=(String)request.getParameter("unique"+j);
			QUpos=(String)request.getParameter("nbrPosU"+j);
			int indiceOrdre=j+nbrQLInt+nbrQMInt;
			
			QUO=    (String)request.getParameter("QUO"+indiceOrdre);
			
			QU.put(QUparam,QUpos);
			tm.put(QUO+"QU",QUparam);
			}
		
	
		Quest ce = new Quest();
		
		ce.getTitle(titre);
		ce.getNbrQL(nbrQL);
		ce.getNbrQM(nbrQM);
		ce.getNbrQU(nbrQU);
		ce.getQL(QL);
		ce.getQM(QM);
		ce.getQU(QU);
		ce.getTm(tm);
		request.setAttribute("titre", titre);
		request.setAttribute("nbrQL", nbrQL);
		request.setAttribute("nbrQM", nbrQM);
		request.setAttribute("nbrQU", nbrQU);
		request.setAttribute("QL", QL);
		request.setAttribute("QM", QM);
		request.setAttribute("QU", QU);
		request.setAttribute("TM", tm);
		RequestDispatcher view = request.getRequestDispatcher("final.jsp");
		
		view.forward(request, response); 
        

	}

}
